Skip to content

Method: createStatement(Resource, IRI, Value, IRI)

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.ontodriver.sesame;
14:
15: import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
16: import cz.cvut.kbss.ontodriver.model.Assertion;
17: import cz.cvut.kbss.ontodriver.model.NamedResource;
18: import cz.cvut.kbss.ontodriver.model.Value;
19: import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
20: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
21: import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
22: import org.eclipse.rdf4j.model.IRI;
23: import org.eclipse.rdf4j.model.Resource;
24: import org.eclipse.rdf4j.model.Statement;
25:
26: import java.net.URI;
27: import java.util.*;
28:
29: class AxiomSaver {
30:
31: private final Connector connector;
32: private final SesameValueConverter valueConverter;
33:
34: AxiomSaver(Connector connector) {
35: this.connector = connector;
36: this.valueConverter = new SesameValueConverter(connector.getValueFactory());
37: }
38:
39: void persistAxioms(AxiomValueDescriptor axiomDescriptor) throws SesameDriverException {
40: final List<Statement> statements = new ArrayList<>();
41: for (Assertion assertion : axiomDescriptor.getAssertions()) {
42: statements.addAll(createSesameStatements(axiomDescriptor.getSubject(), assertion,
43: axiomDescriptor.getAssertionValues(assertion),
44: axiomDescriptor.getAssertionContext(assertion)));
45: }
46: if (!statements.isEmpty()) {
47: connector.addStatements(statements);
48: }
49: }
50:
51: void persistAxioms(NamedResource subject, Map<Assertion, Set<Value<?>>> values, URI context)
52: throws SesameDriverException {
53: final List<Statement> statements = new ArrayList<>();
54: for (Map.Entry<Assertion, Set<Value<?>>> entry : values.entrySet()) {
55: statements.addAll(createSesameStatements(subject, entry.getKey(), entry.getValue(), context));
56: }
57: if (!statements.isEmpty()) {
58: connector.addStatements(statements);
59: }
60: }
61:
62: private Collection<? extends Statement> createSesameStatements(NamedResource subject,
63: Assertion assertion,
64: Collection<Value<?>> assertionValues,
65: URI assertionContext)
66: throws SesameDriverException {
67: final List<Statement> statements = new ArrayList<>(assertionValues.size());
68:
69: final Resource subjectUri = SesameUtils.toSesameIri(subject.getIdentifier(), connector.getValueFactory());
70: final IRI property = SesameUtils.toSesameIri(assertion.getIdentifier(), connector.getValueFactory());
71: final IRI context = assertionContext != null ? SesameUtils.toSesameIri(assertionContext, connector.getValueFactory()) : null;
72: for (Value<?> val : assertionValues) {
73: if (val == Value.nullValue()) {
74: continue;
75: }
76: org.eclipse.rdf4j.model.Value value = valueConverter.toSesameValue(assertion, val);
77: statements.add(createStatement(subjectUri, property, value, context));
78: }
79: return statements;
80: }
81:
82: private Statement createStatement(Resource subject, IRI property, org.eclipse.rdf4j.model.Value value,
83: IRI context) {
84:• if (context != null) {
85: return connector.getValueFactory().createStatement(subject, property, value, context);
86: } else {
87: return connector.getValueFactory().createStatement(subject, property, value);
88: }
89: }
90: }